<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <!-- <el-col :span="12">
          <el-form-item :label="$t('阶段')" prop="phase">
            <el-input v-model="form.phase" :placeholder="$t('阶段')" show-word-limit maxlength="5"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('月数')" prop="months">
            <el-input v-model="form.months" :placeholder="$t('月数')" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开始时间')" prop="startDate">
            <el-date-picker
              v-model="form.startDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('开始时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('结束时间')" prop="endDate">
            <el-date-picker
              v-model="form.endDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('结束时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计提基数')" prop="accrualBase">
            <el-input v-model="form.accrualBase" :placeholder="$t('计提基数')" show-word-limit maxlength="10"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('天数')" prop="days">
            <el-input v-model="form.days" :placeholder="$t('天数')" show-word-limit/>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item :label="$t('是否固定管理费费率')" prop="whetherFixed">
            <el-radio-group v-model="form.whetherFixed" @change="updateChangeData">
              <el-radio
                v-for="dict in whetherFixedOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherFixed === 'Y'" :span="12">
          <el-form-item :label="$t('管理费费率(%)')" prop="managementRate">
            <Ratio v-model="form.managementRate" :controls="false" :min="0" :max="100" :precision="2" />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.whetherFixed === 'N'">
        <div class="area-header">
          <span class="area-header-title">{{ $t('出资人费率列表') }}</span>
        </div>
        <el-table :data="form.fundManagementPhaseInvestorList" border class="nowrapTable formTable">
          <el-table-column :label="$t('出资人名称')" align="center" prop="investorName" show-overflow-tooltip />
          <el-table-column :label="$t('费率(%)')" align="center">
            <template slot-scope="scope">
              <el-form-item :prop="'fundManagementPhaseInvestorList.' + scope.$index + '.managementRate'" :rules="fundManagementPhaseInvestorListRules.managementRate">
                <Ratio v-model="scope.row.managementRate" :controls="false" :min="0" :max="100" :precision="2" />
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <el-button @click="cancel">{{ $t('取消') }}</el-button>
      <el-button type="primary" @click="submitForm">{{ $t('确定') }}</el-button>
    </div>
  </div>
</template>

<script>
import {
  // saveFundManagementPhase,
  getFundManagementPhase,
  getFundInvestorListByFundId
} from '@/api/fund/managementfee/fundManagementPhase'

export default {
  data() {
    return {
      // 表单参数
      form: {
        fundManagementPhaseInvestorList: []
      },
      // 遮罩层
      loading: false,
      whetherFixedOptions: [],
      // 表单校验
      rules: {
        whetherFixed: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        managementRate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      fundManagementPhaseInvestorListRules: {
        managementRate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      currentWhetherFixed: undefined
    }
  },
  created() {
    this.getDicts('sys_yes_no').then(response => {
      this.whetherFixedOptions = response.data
    })
  },
  mounted() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined && data.id !== null) {
        getFundManagementPhase(data.id).then(response => {
          this.form = response.data
          this.currentWhetherFixed = this.form.whetherFixed
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        // this.reset()
        this.form = data
        this.currentWhetherFixed = this.form.whetherFixed
        console.log('initFee', this.form)
        if (this.form.whetherFixed === 'N' && this.form.fundManagementPhaseInvestorList.length === 0) {
          this.initInvestorList(this.$route.query.id)
        }
        this.loading = false
      }
    },
    // init出资人费率列表
    initInvestorList(fundId) {
      getFundInvestorListByFundId(fundId).then(response => {
        this.form.fundManagementPhaseInvestorList = response.data
      })
    },
    updateChangeData(whetherFixed) {
      if (whetherFixed === 'Y') {
        this.form.fundManagementPhaseInvestorList = []
      } else if (whetherFixed === 'N') {
        this.initInvestorList(this.$route.query.id)
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        managementId: undefined,
        phase: undefined,
        months: undefined,
        startDate: undefined,
        endDate: undefined,
        accrualBase: undefined,
        days: undefined,
        managementRate: undefined,
        whetherFixed: undefined,
        fundManagementPhaseInvestorList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.form.whetherFixed = this.currentWhetherFixed
      this.$parent.$parent.rateOpen = false
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.rateOpen !== undefined) {
        this.$parent.$parent.rateOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$emit('setInvestRate', this.form)
          // this.loading = true
          // saveFundManagementPhase(this.form).then(response => {
          //   if (response.code === 200) {
          //     this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
          //   } else {
          //     this.msgError(response.msg)
          //   }
          //   this.loading = false
          // })
        } else {
          this.locationError(document)
        }
      })
    }
  }
}
</script>
